<template>
	<div class="widgets bg-white">
		<el-input v-model.trim="searchText" placeholder="输入搜索内容" class="search__input" @keyup.enter.native="search"></el-input>
        <el-button type="primary" @click="search">查询</el-button>
        <div class="btn-group right">
	        <el-button type="info" @click="refresh">刷新</el-button>
	        <el-button type="primary" v-if="!isAdd" disabled>新增</el-button>
	        <el-button type="primary" v-else @click="add">新增</el-button>
	        <el-button type="danger" v-if="multipleSelection.length <= 0" disabled>删除</el-button>
	        <el-button type="danger" v-else @click="del">删除</el-button>
	        <el-button type="danger" v-if="multipleSelection.length <= 0" disabled>禁用</el-button>
	        <el-button type="danger" v-else @click="disabedBtn(0, '禁用')">禁用</el-button>
	        <el-button type="success" v-if="multipleSelection.length <= 0" disabled>启用</el-button>
	        <el-button type="success" v-else @click="disabedBtn(1, '启用')">启用</el-button>
	        <el-button-group class="btn-other">
	            <el-button v-if="multipleSelection.length <= 0" disabled>
	                <img src="../../../assets/images/export.png" alt="">
	            </el-button>
	            <el-button title="导出" v-else @click="exportExcel">
	                <img src="../../../assets/images/export.png" alt="">
	            </el-button>
	            <el-button title="导入">
                    <el-upload class="upload-demo" :action="$url + 'supplier/importSupplier?systemId='+ systemId" :show-file-list="false" multiple style="display: inline-block;" :on-success="importExcelSuccess">
                        <img src="../../../assets/images/import.png" alt="">
                    </el-upload>
                </el-button>                 
	            <el-button title="打印">
	                <img src="../../../assets/images/print.png" alt="">
	            </el-button>
	            <el-button title="帮助">
	                <img src="../../../assets/images/help.png" alt="">
	            </el-button>
	        </el-button-group>
	    </div>
	</div>
</template>
<script>
export default {
	data() {
		return {
			searchText: '',
			multipleSelection: this.$store.state.supplierStore.multipleSelection,
            selectRowId: this.$store.state.supplierStore.selectRowId,
            isAdd: true,
            row: this.$store.state.supplierStore.row,
		}
	},
	computed: {
		systemId() {
			return this.$store.state.systemId;
		}
	},
	watch: {
        row(newVal) {
        	newVal[0]['level'] == 0 ? this.isAdd = true : this.isAdd = false;
        }
    },
	methods: {
		search() {},
		refresh() {
			const loading = this.$loading({
                lock: true,
                text: '数据刷新中...',
                spinner: 'el-icon-loading',
                background: '#fff'
            });
            setTimeout(() => {
                loading.close();
            }, 1000);
		},
		add() {
			this.$store.commit('supplierStore/updateRowId');
			this.$util.openLayerIframe({title: '供应商-新增', zIndex: 13, width: '550px', height: '80%', url: this.$domain + '/#/basicData/supplier/form'});
		},
		del() {			//删除
			this.$confirm('确定删除选中数据?', '删除提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http.delete('supplier/delete', {data: this.selectRowId}).then(res => {
                    if(res.data.code == 20001) {
                        this.$message({message: '删除成功', type: 'success', duration: 1500});
                        this.$store.commit('supplierStore/updateExpandNodeId', this.multipleSelection[0]['typeId']);     //根据客户大类来展开节点
                        setTimeout(() => {
                           this.$store.dispatch('supplierStore/getTreeData');
                        }, 1000);
                    } else {
                        this.$message.error(res.data.msg || '删除失败');
                    }
                });
            }).catch(() => {
                this.$message({message: '已取消删除', type: 'info', duration: 1500});
            });
		},
		disabedBtn(status, msg) {			//禁用启用
			var type = {
                headers: {
                    'Content-type': 'application/json;charset=UTF-8'
                }
            }
            for(var i = 0; i < this.multipleSelection.length; i++) {
                if(this.multipleSelection[i]['state'] == status) {
                    this.$alert(msg + '失败，其中包含已'+ msg +'选项，重新选择！', msg + '提示', {
                        confirmButtonText: '确定'
                    });
                    return false;
                }
            }
            this.$http.post('supplier/updateState?state=' + status, JSON.stringify(this.selectRowId), type).then(res => {
                if(res.data.code == 20001) {
                    this.$message({message: msg + '成功', type: 'success', duration: 1500});
                    setTimeout(() => {
                        this.$store.dispatch('supplierStore/getTreeData');
                    }, 1000);
                } else {
                    this.$message.error(res.data.msg || '禁用失败');
                }
            });
		},
		exportExcel() {			//导出
			var idSting = this.selectRowId.join(',');           //将[1,2,3]格式转为 "1,2,3"
            this.$http.get('supplier/export',{params: {idStrs: idSting}}).then(res => {
                this.$message({message: '导出成功', type: 'success', duration: 1500});
                window.location.href = this.$url + 'supplier/export?idStrs=' + idSting;          //导出时触发
            }).catch(err => {
                this.$message.error(err);
            });
		},
		importExcelSuccess(res){                //导入
            if(res.code === 20001) {
                this.$message({message: '导入成功', type: 'success', duration: 1500});
                setTimeout(() => {
                    this.$store.dispatch('supplierStore/getTreeData');
                }, 1000);
            } else {
                this.$message({message: res.msg, type: 'error',duration: 1500});
            }
        },
	}
}
</script>